package com.shop.api;

import com.shop.po.dto.GoodsInfo;
import com.shop.merchant.service.GoodsBaseService;
import com.shop.merchant.service.MerchantService;
import com.shop.po.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author banxia
 * @since 2023-02-24
 */
@RestController
@RequestMapping("/goods")
@Validated
@CrossOrigin
public class GoodsBaseController {

    @Autowired
    private GoodsBaseService goodsBaseService;

    //提供上传图片功能
    @Autowired
    private MerchantService merchantService;

    //查询该商家某些商家的总数
    @GetMapping("/getTotal")
    public R getTotal(@RequestParam(value = "name",required = false) String name,
                      @RequestParam(value = "category",required = false)Long categoryId){
        Integer count=goodsBaseService.getTotal(name,categoryId);
        return R.ok().data("count",count);
    }

    //提供feign功能，修改商品库存
    @PostMapping("/goodsCountUpdate/{goodsId}/{count}")
    public R goodsCountUpdate(@PathVariable("goodsId") Long goodsId,
                              @PathVariable("count") Long count){
        goodsBaseService.goodsCountUpdate(goodsId,count);
        return R.ok("更新库存成功");
    }


    //获取商品的详情信息
    @GetMapping("/getByGoodsIdFromRedis")
    public R getByGoodsIdFromRedis(Long id){
        GoodsInfo goodsInfo = goodsBaseService.getByGoodsIdFromRedis(id);
        return R.ok("成功获取商品详情数据").data("goodsInfo",goodsInfo);
    }

    @GetMapping("/getGoodsList")
    public R getGoodsList(
            @RequestParam(value = "likeName",required = false) String likeName,
            @RequestParam(value = "minPrice",required = false) Long minPrice,
            @RequestParam(value = "maxPrice",required = false) Long maxPrice,
            @RequestParam(value = "categoryName",required = false) String categoryName,
            @Valid @Min(0)@Max(2000) @RequestParam(value = "page",required = true) Integer page,
            @Valid @Min(0)@Max(20)@RequestParam(value = "size",required = true) Integer size){
        List<GoodsInfo> goodsList = goodsBaseService.getGoodsList(
                likeName,
                minPrice,
                maxPrice,
                categoryName,
                page,
                size);
        return R.ok("成功获取数据").data("list",goodsList);
    }


    //这里注意：type为原先的状态，这里要对其进行修改
    //type为0或1
    @PostMapping("/changGoodsStatus/{id}/{type}")
    public R changGoodsStatus(@PathVariable Long id,
                              @Valid
                              @Max(value = 1,message = "type不可以超过1")
                              @Min(value=0,message = "type不可以小于0") @PathVariable Integer type){
        if (type==0) type=1;
        else if (type==1) type=0;
        goodsBaseService.changGoodsStatus(id,type);
        return R.ok("商品状态修改成功").data("type",type);
    }

    @PostMapping("/upload")
    public R uploadGoodsImage(@RequestParam("file") MultipartFile file){
        R r = merchantService.uploadFile(file, "goodsUploadFile");
        return r;
    }


    @PostMapping("/publishGoods")
    public R publishGoods(@RequestBody GoodsInfo goodsInfo){
        goodsBaseService.publishGoods(goodsInfo);
//        return R.ok().data("goods",goodsInfo);
        return R.ok("成功上架商品");
    }

    @PostMapping("/deleteGoods/{id}")
    public R deleteGoods(@PathVariable("id") Long id){
        goodsBaseService.deleteGoods(id);
        return R.ok("删除成功");
    }

    @GetMapping("/UserGetTotal")
    public R UserGetTotal(@RequestParam(value = "likeName",required = false) String likeName,
                      @RequestParam(value = "minPrice",required = false) Long minPrice,
                      @RequestParam(value = "maxPrice",required = false) Long maxPrice,
                      @RequestParam(value = "categoryName",required = false) String categoryName){
        Long count=goodsBaseService.UserGetTotal(likeName,minPrice,maxPrice,categoryName);
        return R.ok().data("count",count);
    }





}
/*
    @GetMapping("/test")
    public R test(){
        GoodsInfo goodsInfo = new GoodsInfo();
        //设置分类
        goodsInfo.setCategoryName("衣服");
        //设置商品基本信息
        goodsInfo.setName("羽绒服");
        goodsInfo.setDescription("贼保暖");
        goodsInfo.setCount(1000L);
        //设置商品销售
        goodsInfo.setOldPrice(1000L);
        goodsInfo.setNewPrice(200L);
        goodsInfo.setIsPromotion(1);
        goodsInfo.setPromotionDescrption("双11特大活动");
        //设置图片
        List<GoodsImage> listImages=new ArrayList<>();
        String[] paths={"www","baidu","com"};
        String[] descr={"3w","百度","com"};
        for (int i = 0; i < paths.length; i++) {
            GoodsImage goodsImage = new GoodsImage();
            goodsImage.setImagePath(paths[i]);
            goodsImage.setDescription(descr[i]);
            listImages.add(goodsImage);
        }
        goodsInfo.setImageList(listImages);
        goodsBaseService.publishGoods(goodsInfo);
        return R.ok().data("data",goodsInfo);
    }
 */

